/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package group2assignone.view;

import group2assignone.func.UCCtoALAdapter;
import group2assignone.model.Manager;
import group2assignone.ucm.UCCloseWindow;
import group2assignone.ucm.UCLogout;
import group2assignone.ucm.manager.UCShowUserDetails;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Flick
 */
public class ClientListView extends OurView {

    private JButton viewButton = new JButton("View Details");
    private JButton cancelButton = new JButton("Close Window");
    private JList clientList = new JList();
    private ClientTable table;
    private JTable clientTable;
    private JScrollPane tableScroll = new JScrollPane();
    private int selectedRow;
    private Manager man;
    private JLabel manLab;

    public ClientListView(ArrayList<String> cList, Manager man) {
        table = new ClientTable(cList);
        this.man = man;
        manLab = new JLabel(man.getUsername());
    }

    @Override
    public void go() {
        super.go();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    @Override
    public void addComponents() {
        setTitle("Members who are currently logged in");

        northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.LINE_AXIS));
        manLab.setAlignmentX(JLabel.LEFT);
        northPanel.add(manLab);

        clientTable = new JTable(table);
        clientTable.setRowSelectionAllowed(true);
        clientTable.setColumnSelectionAllowed(false);
        clientTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tableScroll = (new JScrollPane(clientTable));
        centerPanel.add(tableScroll);

        southPanel.add(viewButton);
        southPanel.add(cancelButton);

        centerPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        northPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        southPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        clientTable.getSelectionModel().addListSelectionListener(new RowListener());
        selectedRow = clientTable.getSelectedRow();

        viewButton.addActionListener(new UCCtoALAdapter(new UCShowUserDetails(this)));
        cancelButton.addActionListener(new UCCtoALAdapter(new UCCloseWindow(this)));
    }

    /**
     * This function returns a boolean result if there is a selected manager
     * @return
     * return true if is a selection
     * return false if there is no selection
     */
    public boolean isNoSelection() {
        return clientList.isSelectionEmpty();
    }

    /**
     * this function returns the selected row number
     * this is to enable deleting and editing of the correct table row
     * @return
     */
    public String[] getRow() {
        String[] row = new String[clientTable.getColumnCount()];
        int column = 0;

        for (String s : row) {
            System.out.println("col = " + column);
            // s.equals( table.getValueAt(selectedRow, column));
            row[column] = table.getValueAt(selectedRow, column);
            column++;
        }
        return row;

    }

    /**
     * This function returns the string entered into the textfield Username
     * @return
     */
    public String getUserName() {
        int column = 0;
        String userName = table.getValueAt(selectedRow, column);
        return userName;
    }

    /**
     * This function is to set the selected row in program
     * @param row
     */
    public void setSelectedRow(int row) {
        selectedRow = row;

    }

    /**
     * this function is to return the private variable which holds the selected row
     * @return
     */
    public int getSelectedRow() {
        return selectedRow;
    }

    public void updateTable(ArrayList<String> loggedIn) {
        centerPanel.remove(tableScroll);
        clientTable = new JTable(new ClientTable(loggedIn));
        tableScroll = (new JScrollPane(clientTable));
        centerPanel.add(tableScroll);
        centerPanel.revalidate();
    }

    private class RowListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            setSelectedRow(clientTable.getSelectedRow());
        }
    }
}

